package com.gzhryc.shared_device.oem.h5.mng.controllers.market.cloud;

import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.net_marketing.core.enums.EGoodsClassify;
import com.gzhryc.net_marketing.cloud.goods.dao.db.CloudGoodsAuditApply;
import com.gzhryc.net_marketing.cloud.goods.dao.db.CloudGoodsSpaceAuditItem;
import com.gzhryc.net_marketing.cloud.goods.services.CloudGoodsAuditApplyService;
import com.gzhryc.net_marketing.cloud.goods.services.CloudGoodsSpaceAuditItemService;
import com.gzhryc.net_marketing.cloud.goods.services.dto.CloudGoodsAuditApplySearch;
import com.gzhryc.net_marketing.core.enums.EGoodsAuditState;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class GoodsAuditApplyController {

    @Route(value = { "/market/cloud/goods/audit/list.html" })
    public String list(HttpServletRequest request) {
        request.setAttribute("auditStateList", EGoodsAuditState.values());
        request.setAttribute("classifyList", EGoodsClassify.values());
        return "/market/cloud/goods/audit/list.html";
    }

    @Route(value = "/market/cloud/goods/audit/list.data")
    public ListResult<CloudGoodsAuditApply> listData(HttpServletRequest request) {
        ListResult<CloudGoodsAuditApply> result = new ListResult<CloudGoodsAuditApply>(DtoConstants.NULL_DATA, "暂无数据");
        CloudGoodsAuditApplySearch search = WebUtils.getParameter(request, CloudGoodsAuditApplySearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

        result.getBody().setTotal(CloudGoodsAuditApplyService.self().countJoinBySearch(search));
        result.getBody().setData(CloudGoodsAuditApplyService.self().findJoinBySearch(search, pageNo, pageLimit));

        if (result.getBody().getData() != null && result.getBody().getData().size() > 0) {
            result.setCode(DtoConstants.SUCCESS, null);
        }
        return result;
    }

    @Route(value = { "/market/cloud/goods/audit/info.html" })
    public String add(HttpServletRequest request) {
        Long id = WebUtils.getLong(request,"id");
        CloudGoodsAuditApply apply = CloudGoodsAuditApplyService.self().getJoinById(id);
        WebUtils.setAttribute(request,apply);
        Long total = CloudGoodsSpaceAuditItemService.self().countByApplyId(id);
        if(total != null && total > 0){
            request.setAttribute("hasGoodsSpace",true);
        }
        return "/market/cloud/goods/audit/info.html";
    }

    @Route(value = "/market/cloud/goods/audit/space/list.data")
    public ListResult<CloudGoodsSpaceAuditItem> spaceListData(HttpServletRequest request) {
        ListResult<CloudGoodsSpaceAuditItem> result = new ListResult<CloudGoodsSpaceAuditItem>(DtoConstants.NULL_DATA, "暂无数据");
        Long applyId = WebUtils.getLong(request,"applyId");
        List<CloudGoodsSpaceAuditItem> data = CloudGoodsSpaceAuditItemService.self().findByApplyId(applyId);
        if(data != null && data.size() > 0){
            result.setCode(DtoConstants.SUCCESS,null);
            result.getBody().setData(data);
        }
        return result;
    }

    @Route(value = { "/market/cloud/goods/audit/handle.html" })
    public String audit(HttpServletRequest request) {
        request.setAttribute("auditStateList", new EGoodsAuditState[]{EGoodsAuditState.Success, EGoodsAuditState.Fail});
        return "/market/cloud/goods/audit/audit.html";
    }

    @Route(value = { "/market/cloud/goods/audit/handle.do" })
    public AjaxResult doAudit(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "审核处理失败");
        Long id = WebUtils.getLong(request, "id");
        Integer auditState = WebUtils.getInteger(request,"auditState");
        String auditNote = WebUtils.getString(request,"auditNote");
        if (id != null && auditState != null) {
            try {
                if (CloudGoodsAuditApplyService.self().toAudit(id,auditState,auditNote)) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }

    @Route(value = { "/market/cloud/goods/audit/del.do" })
    public AjaxResult doDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除商品失败");
        Long id = WebUtils.getLong(request, "id");
        if (id != null) {
            try {
                if (CloudGoodsAuditApplyService.self().deleteById(id)) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }
}
